package de.jeeyeon;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import de.jeeyeon.util.EmoAdder;

public class FirstActivity extends Activity 
{
	
	private TextView view_date;
	private TextView view_text;
	
	private String author; //me
	private String author2; //u
	
	
	private String[] entrys;
	
	public static final String URL = "http://snuk87.sn.funpic.de/get_lastentry.php";
	public static final String URL2 = "http://snuk87.sn.funpic.de/get_mylastentry.php";

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);
		
		Bundle extras = getIntent().getExtras();
		author = extras.getString("author");
				
		if(author.equals("Dennis"))
		{
			author2 = "Jeeyeon";
		}
		else if(author.equals("Jeeyeon"))
				{
			author2 = "Dennis";
		}
	
		
		view_text = (TextView) findViewById(R.id.textView_entry);
		view_text.setMovementMethod(new ScrollingMovementMethod());
				
		view_date = (TextView) findViewById(R.id.textView_date);	
		
		loadLastEntry();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.first, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
			case R.id.action_menu_addentry:
				Intent i = new Intent(FirstActivity.this, AddActivity.class);
				i.putExtra("author", author);
				startActivity(i);
				return true;
			
			case R.id.action_menu_previous:
				Intent j = new Intent(FirstActivity.this, CalendarActivity.class);
				j.putExtra("author", author2);
				startActivity(j);
				return true;
			
			case R.id.action_menu_my_previous:
				getMyLastEntrys();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	
	private void loadLastEntry()
	{		
		new AsyncTask<Void, Void, String>() 
		{

			@Override
			protected String doInBackground(Void... params) 
			{
				String output = null;
				try
				{		
					DefaultHttpClient client = new DefaultHttpClient();
					HttpPost post = new HttpPost(URL);
					
			        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			        nameValuePairs.add(new BasicNameValuePair("author", author2));
			        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					
					HttpResponse httpResponse = client.execute(post);

					HttpEntity httpEntity = httpResponse.getEntity();
					output = EntityUtils.toString(httpEntity).trim();
					
					int index = output.indexOf("<!");
					
					output = output.substring(0, index);
				}
				catch(Exception e){ e.printStackTrace(); Log.d("Catch: " + e.toString(), "jeejee"); }
				
				return output;
			}
			
			protected void onPostExecute(String result)
			{
				String[] tmp = new String[2];
				
				tmp = result.split("----");
				
				view_date.setText(tmp[0]);
				view_text.setText(EmoAdder.setEmos(tmp[1], getApplicationContext()));
			}
		}.execute();
	}
	
	private void getMyLastEntrys()
	{		
		new AsyncTask<Void, Void, String>() 
		{

			@Override
			protected String doInBackground(Void... params) 
			{
				String output = null;
				try
				{		
					DefaultHttpClient client = new DefaultHttpClient();
					HttpPost post = new HttpPost(URL2);
					
			        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			        nameValuePairs.add(new BasicNameValuePair("author", author));
			        nameValuePairs.add(new BasicNameValuePair("date", getDate()));
			        nameValuePairs.add(new BasicNameValuePair("date2", getDateFromDayBefore()));
			        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					
					HttpResponse httpResponse = client.execute(post);

					HttpEntity httpEntity = httpResponse.getEntity();
					output = EntityUtils.toString(httpEntity).trim();
					
					int index = output.indexOf("<!");
					
					output = output.substring(0, index);
				}
				catch(Exception e){ e.printStackTrace(); Log.d("Catch: " + e.toString(), "jeejee"); }
				
				return output;
			}
			
			protected void onPostExecute(String result)
			{	
				int index = result.indexOf("----");
				int count = Integer.parseInt(result.substring(0, index));
				
				result = result.substring(index + 4);
				if(count > 0)
				{				
					entrys = new String[count];
					
					entrys = result.split("----");
										
					Intent i = new Intent(FirstActivity.this, MyLastEntrysActivity.class);
					i.putExtra("date", getDate());
					i.putExtra("entrys", entrys);
					startActivity(i);					
				}
				else
				{
					Toast.makeText(getApplicationContext(), "No entrys were found", Toast.LENGTH_LONG).show();
				}
			}
		}.execute();
	}
	
	private String getDate()
	{
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat( "dd-MM-yyyy", Locale.GERMANY);
		String datestring = df.format(date);
		
		return datestring;
	}
	
	private String getDateFromDayBefore()
	{
		   Calendar cal = Calendar.getInstance();
		   DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.GERMANY);
		   cal.add(Calendar.DATE, -1);
		   
		   return dateFormat.format(cal.getTime());
	}
	
//	private void setEmos()
//	{
//		String raw = view_text.getText().toString();
//		SpannableString ss = new SpannableString(raw);
//		
//		
//		//emo_1
//		Drawable img = getResources().getDrawable(R.drawable.emo_1);
//		img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());
//		
//		int lastindex = -1;
//		
//		while(raw.contains(":-)"))
//		{
//			if(lastindex > 0)
//			{
//				int index = raw.indexOf(":-)", (lastindex));
//				lastindex = (index + 3);
//				
//				if(index == -1)
//				{
//					break;
//				}
//				else
//				{
//					ss.setSpan(new ImageSpan(img, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				}
//			}
//			else
//			{
//				int index = raw.indexOf(":-)");
//				ss.setSpan(new ImageSpan(img, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				lastindex = (index + 3);
//			}
//		}
//		
//		//emo_2
//		
//		Drawable img2 = getResources().getDrawable(R.drawable.emo_2);
//		img2.setBounds(0, 0, img2.getIntrinsicWidth(), img2.getIntrinsicHeight());
//		
//		lastindex = -1;
//		
//		while(raw.contains(";-)"))
//		{
//			if(lastindex > 0)
//			{
//				int index = raw.indexOf(";-)", (lastindex));
//				lastindex = (index + 3);
//				
//				if(index == -1)
//				{
//					break;
//				}
//				else
//				{
//					ss.setSpan(new ImageSpan(img2, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				}
//			}
//			else
//			{
//				int index = raw.indexOf(";-)");
//				ss.setSpan(new ImageSpan(img2, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				lastindex = (index + 3);
//			}
//		}
//		
//		//emo_3
//		Drawable img3 = getResources().getDrawable(R.drawable.emo_3);
//		img3.setBounds(0, 0, img3.getIntrinsicWidth(), img3.getIntrinsicHeight());
//		
//		lastindex = -1;
//		
//		while(raw.contains(";-P"))
//		{
//			if(lastindex > 0)
//			{
//				int index = raw.indexOf(";-P", (lastindex));
//				lastindex = (index + 3);
//				
//				if(index == -1)
//				{
//					break;
//				}
//				else
//				{
//					ss.setSpan(new ImageSpan(img3, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				}
//			}
//			else
//			{
//				int index = raw.indexOf(";-P");
//				ss.setSpan(new ImageSpan(img3, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				lastindex = (index + 3);
//			}
//		}
//		
//		//emo_4
//		
//		Drawable img4 = getResources().getDrawable(R.drawable.emo_4);
//		img4.setBounds(0, 0, img4.getIntrinsicWidth(), img4.getIntrinsicHeight());
//		
//		lastindex = -1;
//		
//		while(raw.contains(":-J"))
//		{
//			if(lastindex > 0)
//			{
//				int index = raw.indexOf(":-J", (lastindex));
//				lastindex = (index + 3);
//				
//				if(index == -1)
//				{
//					break;
//				}
//				else
//				{
//					ss.setSpan(new ImageSpan(img4, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				}
//			}
//			else
//			{
//				int index = raw.indexOf(":-J");
//				ss.setSpan(new ImageSpan(img4, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//				lastindex = (index + 3);
//			}
//		}
//		view_text.setText(ss);
//	}
}
